package me.zhanshi123.enchantfix;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.zhanshi123.enchantfix.metrics.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class EnchantFix extends JavaPlugin {
    private static EnchantFix instance;
    public static String MINECRAFT_VERSION;
    private static ConfigManager configManager;
    private static Protocol protocol;

    public static ConfigManager getConfigManager() {
        return configManager;
    }

    public static Protocol getProtocol() {
        return protocol;
    }

    private Update getUpdate() {
        JsonParser parse = new JsonParser();
        Update update = null;
        try {
            URL url = new URL("https://service.zhanshi123.me/update/index.php?name=EnchantFix");
            InputStream in = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String json = br.readLine();
            JsonObject jsonObject = (JsonObject) parse.parse(json);
            update = new Update(jsonObject.get("name").getAsString(), jsonObject.get("message").getAsString(), jsonObject.get("version").getAsDouble());
        } catch (Exception e) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §c无法获取更新,原因: " + e.getMessage());
                    Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §cUnable to fetch updates , reason: " + e.getMessage());
                }
            }.runTask(instance);
        }
        return update;
    }

    @Override
    public void onEnable() {
        instance = this;
        getMinecraftVersion();
        configManager = new ConfigManager();
        EnchantNameMapper.init();
        protocol = new Protocol();
        protocol.init();
        new BukkitRunnable() {
            @Override
            public void run() {
                Update update = getUpdate();
                if (String.valueOf(update.getVersion()).equalsIgnoreCase(instance.getDescription().getVersion())) {
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §a你正在使用最新版本的插件");
                            Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §aYou are now using the latest version");
                        }
                    }.runTask(instance);
                } else {
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §a" + update.getVersion() + "更新了！更新内容:" + update.getMessage() + " §c快去下载吧!");
                            Bukkit.getConsoleSender().sendMessage("§6§lEnchantFix §7>>> §a" + update.getVersion() + "has released！Content in Chinese:" + update.getMessage() + " §cCheck it out on SpigotMC");
                        }
                    }.runTask(instance);
                }
            }
        }.runTaskLaterAsynchronously(instance, 40L);
        new Metrics(instance);
    }

    public static EnchantFix getInstance() {
        return instance;
    }

    public void getMinecraftVersion() {
        String version = Bukkit.getServer().getClass().getPackage().getName();
        String[] tmp = version.split("\\.");
        version = tmp[tmp.length - 1];
        logToConsole("Your NMS version: §a" + version);
        tmp = version.split("_");
        MINECRAFT_VERSION = tmp[1];
    }

    public void logToConsole(String text) {
        Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "§f[§6EnchantFix§f] " + text));
    }
}
